1
'/************************************* Module Header **************************************\
2 '* Module Name: PaneControl.vb
3 '* Project: VBWordDocument
4 '* Copyright (c) Microsoft Corporation.
6 '* The VBWordDocument project provides the examples on how manipulate Word 2007 Content Controls
7 '* in a VSTO document-level project
9 '* This source is subject to the Microsoft Public License.
10 '* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
11 '* All other rights reserved.
13 '* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
14 '* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
15 '* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
16 '\******************************************************************************************/
18 #Region
"Import directives"
21 Imports System
.Collections
.Generic
22 Imports System
.ComponentModel
23 Imports System
.Drawing
27 Imports System
.Windows
.Forms
28 Imports Microsoft
.VisualStudio
.Tools
.Applications
.Runtime
29 Imports Office
= Microsoft
.Office
.Core
30 Imports Word
= Microsoft
.Office
.Interop
.Word
31 Imports Microsoft
.Win32
36 Public Class PaneControl
38 Dim RangeStart
As Object = 0
39 Dim RangeEnd
As Object = 0
40 Dim selection
As Object = Globals
.ThisDocument
.ThisApplication
.Selection
.Range
42 #Region
"Main Functions"
44 Sub LoadDefaultBuildingBlock()
46 Dim Categorie
As Object = "Built-In"
47 Dim BuildingBlockName
As Object = "Double Table "
48 Dim oTrue
As Object = True
50 'The path of the Building Block.dotx may depends on your opreation system,
51 'We could use HKLM hive to locate the Word.
52 Dim WordPath
As Object
53 WordPath
= Registry
.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\12.0\Word\InstallRoot", "Path", "")
54 Globals
.ThisDocument
.AttachedTemplate
= WordPath
& "Document Parts\1033\Building Blocks.dotx"
55 Dim template
As Word
.Template
= Globals
.ThisDocument
.AttachedTemplate
56 'For example we indert the "Double Table" into our document, we located the specific Building Block via WdBuildingBlockTypes,Categorie and BuildingBlockName.
57 template
.BuildingBlockTypes
.Item(Microsoft
.Office
.Interop
.Word
.WdBuildingBlockTypes
.wdTypeTables
).Categories
.Item(Categorie
).BuildingBlocks
.Item(BuildingBlockName
).Insert(Globals
.ThisDocument
.Application
.Selection
.Range
, oTrue
)
60 Function AddContentControl(ByVal ct
As Word
.WdContentControlType
, ByRef selection
As Object) As Word
.ContentControl
62 Dim units
As Object = Word
.WdUnits
.wdCharacter
63 Dim count
As Object = 1
64 Dim missing
As Object = Type.Missing
66 Dim ContentControl
As Word
.ContentControl
= Globals
.ThisDocument
.ContentControls
.Add(ct
, selection
)
67 ContentControl
.Range
.Select()
68 'by default after we inserted the content control the cursion is within the scope of that control,therefore,use Word OM to move the cursor out of the
69 'new control, and ready to insert the next one.
70 Globals
.ThisDocument
.ThisApplication
.Selection
.MoveRight(units
, count
, missing
)
71 Globals
.ThisDocument
.ThisApplication
.Selection
.TypeParagraph()
72 selection
= Globals
.ThisDocument
.ThisApplication
.Selection
.Range
79 #Region
"Event Handlers"
81 Private Sub btnInsertCC_Click(ByVal sender
As System
.Object, ByVal e
As System
.EventArgs
) Handles btnInsertCC
.Click
82 Globals
.ThisDocument
.Range(RangeStart
, RangeEnd
).Select()
83 AddContentControl(Microsoft
.Office
.Interop
.Word
.WdContentControlType
.wdContentControlBuildingBlockGallery
, selection
)
85 AddContentControl(Microsoft
.Office
.Interop
.Word
.WdContentControlType
.wdContentControlComboBox
, selection
)
87 AddContentControl(Microsoft
.Office
.Interop
.Word
.WdContentControlType
.wdContentControlDate
, selection
)
89 AddContentControl(Microsoft
.Office
.Interop
.Word
.WdContentControlType
.wdContentControlDropdownList
, selection
)
91 AddContentControl(Microsoft
.Office
.Interop
.Word
.WdContentControlType
.wdContentControlPicture
, selection
)
93 AddContentControl(Microsoft
.Office
.Interop
.Word
.WdContentControlType
.wdContentControlRichText
, selection
)
96 Private Sub btnInsertBuiltIn_Click(ByVal sender
As System
.Object, ByVal e
As System
.EventArgs
) Handles btnInsertBuiltIn
.Click
97 Globals
.ThisDocument
.Range(RangeStart
, RangeEnd
).Select()
98 LoadDefaultBuildingBlock()
101 Private Sub btnCCXMLMapping_Click(ByVal sender
As System
.Object, ByVal e
As System
.EventArgs
) Handles btnCCXMLMapping
.Click
102 Globals
.ThisDocument
.Range(RangeStart
, RangeEnd
).Select()
103 'mapping to the value of creator of this document.
104 AddContentControl(Microsoft
.Office
.Interop
.Word
.WdContentControlType
.wdContentControlText
, selection
).XMLMapping
.SetMapping("/ns1:coreProperties[1]/ns0:creator[1]", "", _
105 Globals
.ThisDocument
.CustomXMLParts(1))